﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.TeamFoundation.Build.Client;

namespace ShahyadS.TFSBuildInspectionModel
{
	public class BuildAgentRow : INotifyPropertyChanged
	{
		public Uri Uri { get; set; }
		public bool IsEnabled { get; set; }
		public Uri BuildUri { get; set; }
		
		public string Name { get; set; }
		public string MachineName { get; set; }
		public string ControllerName { get; set; }
		public bool IsControllerEnabled { get; set; }
		public BuildAgentCollection Parent { get; set; }

		public event PropertyChangedEventHandler PropertyChanged;
		internal BuildAgentRow(IBuildAgent agent, BuildAgentCollection parent)
		{
			Parent = parent;
			Refresh(agent);
		}

		private void Refresh(IBuildAgent agent)
		{
			Uri = agent.Uri;
			Name = agent.Name;
			MachineName = agent.MachineName;
			IsEnabled = agent.Enabled;
			ControllerName = agent.Controller.Name;
			IsControllerEnabled = agent.Controller.Enabled;
			BuildUri = agent.ReservedForBuild;
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs("Uri"));
				PropertyChanged(this, new PropertyChangedEventArgs("Name"));
				PropertyChanged(this, new PropertyChangedEventArgs("MachineName"));
				PropertyChanged(this, new PropertyChangedEventArgs("IsEnabled"));
				PropertyChanged(this, new PropertyChangedEventArgs("ControllerName"));
				PropertyChanged(this, new PropertyChangedEventArgs("IsControllerEnabled"));
				PropertyChanged(this, new PropertyChangedEventArgs("BuildUri"));
			}
		}
		internal void Refresh()
		{
			IBuildAgent agent = Parent.BuildServer.GetBuildAgent(Uri);
			Refresh(agent);
		}
	}
}
